package com.hard;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lxk on 2019/7/13.
 */
public class Solution220 {

    static class MetaInfo{
        /**
         * 基数
         */
        private int baseNum;
        /**
         * 范围
         */
        private int range;

        public MetaInfo(int baseNum,int range){
            this.baseNum = baseNum;
            this.range = range;
        }

        public int getBaseNum() {
            return baseNum;
        }

        public int getRange() {
            return range;
        }

        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            if(this==obj){
                return true;
            }else{
                MetaInfo other = (MetaInfo) obj;
                int min = this.baseNum - this.range;
                int max = this.baseNum + this.range;
                if(other.getBaseNum()>=min&&other.getBaseNum()<=max){
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        Map<MetaInfo,Integer> metaInfoPosMap = new HashMap<>();
        for(int i = 0;i < nums.length; i++){
            MetaInfo cur = new MetaInfo(nums[i],t);
            if(metaInfoPosMap.containsKey(cur)){
                int pos = metaInfoPosMap.get(cur);
                if(Math.abs(i-pos)<=k){
                    return true;
                }else{
                    metaInfoPosMap.put(cur,i);
                }
            }else{
                metaInfoPosMap.put(cur,i);
            }
        }
        return false;
    }

}
